package com.example.alibabanacosdiscoveryclient01.controller;

import com.example.alibabanacosdiscoveryclient01.service.CustomerSupportAssistant;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.ai.autoconfigure.chat.memory.cassandra.CassandraChatMemoryProperties;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.document.Document;
import org.springframework.ai.document.DocumentReader;
import org.springframework.ai.reader.pdf.PagePdfDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * @author:guoq
 * @date:2025/2/11
 * @descripion:
 */
@RestController
@RequestMapping("/ai")
@Tag(name = "Advisor应用")
public class AdvisorController {
    private final ChatClient chatClient;
    public AdvisorController(ChatClient.Builder chatClient) {
        this.chatClient=chatClient.build();
    }
    @Autowired
    VectorStore vectorStore;
//    @Autowired
//    public AdvisorController(ChatClient.Builder chatClient,VectorStore vectorStore) {
//        this.chatClient=chatClient
//                .defaultAdvisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults()))
//                .build();
//    }

    @Autowired
    CustomerSupportAssistant assistant;
    @GetMapping("/advisor/rag")
    @Operation(summary = "检索增强RAG")
    public ChatResponse rag(String message) {
        ChatResponse response = chatClient
                .prompt()
//                .advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.defaults()))
                .advisors(new QuestionAnswerAdvisor(vectorStore, SearchRequest.builder().build()))
                .user(message)
                .call()
                .chatResponse();
        return  response;

    }
    @GetMapping("/advisor/rag-expression")
    @Operation(summary = "检索增强RAG-expression")
    public ChatResponse rag2(String message) {
        ChatResponse response = chatClient
                .prompt()
                .advisors(a->a.param(QuestionAnswerAdvisor.FILTER_EXPRESSION,"type=='Spring'"))
                .user(message)
                .call()
                .chatResponse();
        return  response;
    }
    @GetMapping("/advisor/log")
    @Operation(summary = "日志记录")
    public ChatResponse log(String message) {
        ChatResponse response = chatClient
                .prompt()
                .advisors(new SimpleLoggerAdvisor(
                        req->"用户请求:"+req.userText(),
                        resp->"用户响应:"+resp.getResult(),
                        1
                ))
                .user(message)
                .call()
                .chatResponse();
        return  response;
    }
    @GetMapping("/advisor/chatMemory")
    @Operation(summary = "聊天记录")
    public ChatResponse chatRecord(String chatId,String message) {
         return assistant.chat(chatId, message);
    }
}
